package com.learn.project2.service.wx.search;

import com.github.pagehelper.PageHelper;
import com.learn.project2.bean.*;
import com.learn.project2.mapper.MarketGoodsMapper;
import com.learn.project2.mapper.MarketKeywordMapper;
import com.learn.project2.mapper.MarketSearchHistoryMapper;
import com.learn.project2.model.vo.wx.search.SearchIndexVO2;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 曹源
 * @version 1.0
 * @description: 微信小程序 /wx/search的接口的实现方法
 * @date 2022/1/10 15:31
 */
@Service
public class SearchServiceImpl2 implements SearchService2 {

    @Autowired
    MarketKeywordMapper marketKeywordMapper;
    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    /**
     * @description: 微信小程序 /wx/search/index的接口的实现方法
     * @param:
     * @return:
     * @author 曹源
     * @date: 2022-01-10 15:32
     */
    @Override
    public SearchIndexVO2 selectIndex() {
//        private MarketKeyword defaultKeyword;
//        private List<MarketKeyword> hotKeywordList;
////        private List<HistoryKeywordListDTO> historyKeywordList;
        SearchIndexVO2 searchIndexVO = new SearchIndexVO2();
        //查询热门关键词defaultKeyword;
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = marketKeywordExample.createCriteria();
        criteria.andIsDefaultEqualTo(true).andDeletedEqualTo(false);
        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(marketKeywordExample);
        searchIndexVO.setDefaultKeyword(marketKeywords.get(0));

        //查询热门关键词hotKeyword
        PageHelper.startPage(1,3);
        MarketKeywordExample marketKeywordExample1 = new MarketKeywordExample();
        marketKeywordExample1.setOrderByClause("add_time desc");
        MarketKeywordExample.Criteria criteria1 = marketKeywordExample1.createCriteria();
        criteria1.andIsHotEqualTo(true).andDeletedEqualTo(false);
        List<MarketKeyword> marketKeywords1 = marketKeywordMapper.selectByExample(marketKeywordExample1);
        searchIndexVO.setHotKeywordList(marketKeywords1);


        //查询历史关键词historyKeyword
        MarketUser user = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        PageHelper.startPage(1,5);
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        marketSearchHistoryExample.setOrderByClause("update_time desc");
        MarketSearchHistoryExample.Criteria criteria2 = marketSearchHistoryExample.createCriteria();
        //判断是否是匿名请求
        if(user!=null){
            criteria2.andUserIdEqualTo(user.getId()).andDeletedEqualTo(false);
        }else{criteria2.andUserIdEqualTo(0).andDeletedEqualTo(false);}

        List<MarketSearchHistory> marketSearchHistories = marketSearchHistoryMapper.selectByExample(marketSearchHistoryExample);
        List<SearchIndexVO2.HistoryKeywordListDTO> historyKeywordListDTOS= new ArrayList<>();
        for (MarketSearchHistory marketSearchHistory : marketSearchHistories) {
            SearchIndexVO2.HistoryKeywordListDTO historyKeywordListDTO = new SearchIndexVO2.HistoryKeywordListDTO();
            historyKeywordListDTO.setKeyword(marketSearchHistory.getKeyword());
            historyKeywordListDTOS.add(historyKeywordListDTO);
        }
        searchIndexVO.setHistoryKeywordList(historyKeywordListDTOS);
        return searchIndexVO;
    }
    /**
     * @description:  微信小程序 /wx/search/helper的接口方法实现
     * @param:
     * @return:
     * @author 曹源
     * @date: 2022-01-10 17:21
     */
    @Override
    public  ArrayList<String> searchHelper(String keyword) {
        ArrayList<String> helpers = new ArrayList<>();
        //查询匹配的关键词的商品
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        marketGoodsExample.setOrderByClause("name");
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andNameLike("%"+keyword+"%").andDeletedEqualTo(false);
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);

        marketGoods.forEach(item->helpers.add(item.getName()));

        return helpers;
    }
/**
 * @description:  微信小程序 /wx/search/clearhistory的接口方法实现
 * @param:
 * @return:
 * @author 曹源
 * @date: 2022-01-10 17:21
 */
    @Override
    public void deletehistory() {
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        MarketSearchHistoryExample.Criteria criteria = marketSearchHistoryExample.createCriteria();

        MarketUser user = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        if(user!=null){
            criteria.andUserIdEqualTo(user.getId()).andDeletedEqualTo(false);
        }else{criteria.andUserIdEqualTo(0).andDeletedEqualTo(false);}

        MarketSearchHistory marketSearchHistory = new MarketSearchHistory();
        marketSearchHistory.setDeleted(true);
        marketSearchHistoryMapper.updateByExampleSelective(marketSearchHistory,marketSearchHistoryExample);
    }
}
